/**
 * 
 */
package com.java.test.system.controller;

import java.time.LocalDateTime;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.java.test.controller.BaseController;
import com.java.test.system.dto.LogInfo;
import com.java.test.system.dto.LoginAccountInfo;
import com.java.test.system.dto.MenuInfo;
import com.java.test.system.dto.PowerInfo;
import com.java.test.system.dto.RoleInfo;
import com.java.test.util.HttpErrorCode;
import com.java.test.util.JsonResult;
import com.java.test.util.PageInfo;

/**
 * @author 系统
 *
 */
@RestController
@RequestMapping("/system")
public class SystemMangeController extends BaseController {

	/**
	 * 菜单列表
	 * 
	 * @param info
	 * @return
	 */
	@RequestMapping("/menuallList")
	public JsonResult menuallList() {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			PowerInfo info = systemManageService.selectallMenuList();
			if (info == null) {
				js.setCode(HttpErrorCode.REQUESTSUCCESS.getCode());
				js.setMessage(HttpErrorCode.PARAMERROR.getContent());
				return js;
			}
			js.setData(info);
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	/**
	 * 角色列表
	 * 
	 * @param info
	 * @return
	 */
	@RequestMapping("/roleList")
	public JsonResult roleList(@RequestBody RoleInfo info) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			List<RoleInfo> list = systemManageService.selectroleList(info);
			if (list == null) {
				js.setCode(HttpErrorCode.REQUESTSUCCESS.getCode());
				js.setMessage(HttpErrorCode.PARAMERROR.getContent());
				return js;
			}
			js.setData(list);
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	/**
	 * 单个角色查询
	 * 
	 * @param info
	 * @return
	 */
	@RequestMapping("/selectrolebyId")
	public JsonResult selectrolebyId(@RequestBody RoleInfo info) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			RoleInfo infos = systemManageService.selectrolebyId(info);
			if (infos == null) {
				js.setCode(HttpErrorCode.REQUESTSUCCESS.getCode());
				js.setMessage(HttpErrorCode.PARAMERROR.getContent());
				return js;
			}
			// 得到角色的权限
			List<MenuInfo> list = systemManageService.selectloginuserMenu(infos.getId());
			infos.setList(list);
			js.setData(infos);
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	/**
	 * 添加角色
	 * 
	 * @param info
	 * @return
	 */
	@RequestMapping("/addroleInfo")
	public JsonResult addroleInfo(@RequestBody RoleInfo info, HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");
			if (user != null) {
				info.setAddPeople(user.getLoginAccout());
				info.setCreateTime(LocalDateTime.now());
			}
			int i = systemManageService.insertroleInfo(info);
			if (i <= 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			}

			// log

			systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(), "系统管理中添加角色");

		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	/**
	 * 修改角色
	 * 
	 * @param info
	 * @return
	 */
	@RequestMapping("/updateroleInfo")
	public JsonResult updateroleInfo(@RequestBody RoleInfo info, HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			if (info.getHighestAuthority() == 1) {
				js.setCode(HttpErrorCode.SYSTEMUSERERROR.getCode());
				js.setMessage(HttpErrorCode.SYSTEMUSERERROR.getContent());
				return js;
			}

			int i = systemManageService.updateroleInfo(info);
			if (i <= 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			}
			// log
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");
			systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(), "系统管理中修改角色");

		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	/**
	 * 删除角色
	 * 
	 * @param info
	 * @return
	 */
	@RequestMapping("/deleteroleInfo")
	public JsonResult deleteroleInfo(@RequestBody RoleInfo info, HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			if (info.getHighestAuthority() == 1) {
				js.setCode(HttpErrorCode.SYSTEMUSERERROR.getCode());
				js.setMessage(HttpErrorCode.SYSTEMUSERERROR.getContent());
				return js;
			}

			int i = systemManageService.deleteroleInfo(info);
			if (i <= 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			}

			// log
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");
			systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(), "系统管理删除角色");

		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	/*************** ====== 账号 === *************************/

	/**
	 * 账号分页查询
	 * 
	 * @param info
	 * @return
	 */
	@RequestMapping("/loginaccountList")
	public JsonResult loginaccountList(@RequestBody PageInfo<LoginAccountInfo> info) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			PageInfo<LoginAccountInfo> list = systemManageService.selectaccountList(info);
			if (list == null) {
				js.setCode(HttpErrorCode.REQUESTSUCCESS.getCode());
				js.setMessage(HttpErrorCode.PARAMERROR.getContent());
				return js;
			}
			js.setData(list);
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	/**
	 * 添加登陆账号
	 * 
	 * @param info
	 * @return
	 */
	@RequestMapping("/insertloginaccountInfo")
	public JsonResult insertloginaccountInfo(@RequestBody LoginAccountInfo info, HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			// 添加账号前面判断账号名字是否有重复
			
			LoginAccountInfo checkinfo  = systemManageService.loginuserisexits(info.getLoginAccout());
			if(checkinfo !=null) {
				js.setCode(HttpErrorCode.USERISEXITS.getCode());
				js.setMessage(HttpErrorCode.USERISEXITS.getContent());
				return js;
			}

			int i = systemManageService.insertloginaccountInfo(info);
			if (i <= 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			}
			// log
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");
			systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(),
					"系统管理添加登陆账号" + info.getLoginAccout());
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	/**
	 * 删除登陆账号
	 * 
	 * @param info
	 * @return
	 */
	@RequestMapping("/deleteloginaccountInfo")
	public JsonResult deleteloginaccountInfo(@RequestBody List<LoginAccountInfo> info, HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			int ids[] = info.stream().mapToInt(LoginAccountInfo::getId).toArray();
			int i = systemManageService.deleteloginaccountInfo(ids);
			if (i <= 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			}
			// log
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");
			systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(), "系统管理删除登陆账号");
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	/**
	 * 启用登陆账号
	 * 
	 * @param info
	 * @return
	 */
	@RequestMapping("/updateloginaccountInfouse")
	public JsonResult updateloginaccountInfouse(@RequestBody List<LoginAccountInfo> info, HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			int ids[] = info.stream().mapToInt(LoginAccountInfo::getId).toArray();
			int i = systemManageService.updateloginaccountInfouse(ids);
			if (i <= 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			}
			// log
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");
			systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(), "系统管理启用登陆账号");
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	/**
	 * 禁用登陆账号
	 * 
	 * @param info
	 * @return
	 */
	@RequestMapping("/updateloginaccountInfonotuse")
	public JsonResult updateloginaccountInfonotuse(@RequestBody List<LoginAccountInfo> info,
			HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			int ids[] = info.stream().mapToInt(LoginAccountInfo::getId).toArray();
			int i = systemManageService.updateloginaccountInfonotuser(ids);
			if (i <= 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			}
			// log
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");
			systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(), "系统管理禁用登陆账号");
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	/**
	 * 禁用登陆账号
	 * 
	 * @param info
	 * @return
	 */
	@RequestMapping("/updateloginaccountPassword")
	public JsonResult updateloginaccountPassword(@RequestBody LoginAccountInfo info, HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {

			if (info.getLoginAccout() == null || "".equals(info.getLoginAccout()) || info.getPassWord() == null
					|| "".equals(info.getPassWord()) || info.getNewpaassWord() == null
					|| "".equals(info.getNewpaassWord())) {
				js.setCode(HttpErrorCode.PARAMERROR.getCode());
				js.setMessage(HttpErrorCode.PARAMERROR.getContent());
				return js;
			}
			LoginAccountInfo oldinfo = systemManageService.loginuser(info.getLoginAccout(), info.getPassWord());
			if (oldinfo == null) {
				js.setCode(HttpErrorCode.OLDERROR.getCode());
				js.setMessage(HttpErrorCode.OLDERROR.getContent());
				return js;
			}

			if (info.getPassWord().equals(info.getNewpaassWord())) {
				js.setCode(HttpErrorCode.OLDPASSWORDNEWPASSWORDSAMEERROR.getCode());
				js.setMessage(HttpErrorCode.OLDPASSWORDNEWPASSWORDSAMEERROR.getContent());
				return js;
			}
			info.setPassWord(info.getNewpaassWord());
			int i = systemManageService.updateloginaccountPassword(info);
			if (i <= 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			}
			// log
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");
			systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(),
					"修改登录账号" + info.getLoginAccout() + "密码");
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	@RequestMapping("/loginLogList")
	public JsonResult loginLogList(@RequestBody PageInfo<LogInfo> info) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			PageInfo<LogInfo> list = systemManageService.selectlogList(info);
			if (list == null) {
				js.setCode(HttpErrorCode.REQUESTSUCCESS.getCode());
				js.setMessage(HttpErrorCode.PARAMERROR.getContent());
				return js;
			}
			js.setData(list);
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

}
